package com.uni.sofia.reader.magazine
{
	import com.uni.sofia.reader.interfaces.Comparable;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.Image;
	
	import org.flexunit.internals.ArrayComparisonFailure;

	[Bindable]
	/**
	 * The basic class for a magazine.
	 */ 
	public class Magazine extends EventDispatcher
	{
		/**
		 * 
		 */ 
		public var name:String;
		public var publisher:String;
		public var released:Date;
		public var front:Image;
		
		[ArrayElementType("Page")]
		/**
		 *The container of the pages (should be private) 
		 */ 
		public var pages:ArrayCollection;
		
		/**
		 * The basic class for a magazine.
		 */ 
		public function Magazine()
		{
			pages = new ArrayCollection();
		}
		
		public function get numberOfPages():int
		{
			return pages.length;
		}
		
		/**Adds a page to a magazine, if there is such a page in the magazine, the page is NOT added
		 * @param newPage
		 * 		the new page instance to be added
		 */ 
		public function addPage(newPage:Page):Page
		{
			if(newPage == null || !(newPage is Page) || pages.contains(newPage)) return null;
			
			for(var i:int = 0; i < pages.length; i++)
			{
				if( newPage.equals(pages.getItemAt(i)))
				{
					return null;
				}
			}
			
			pages.addItem(newPage);
			dispatchEvent(new Event("dataChange"));
			return newPage;
		}
		
		/**
		 * gets concrete page of the magezine, this method will come in hand for showing individual pages
		 *@param index
		 * 		the number of the page, for example if index==0 this is the front cover
		 */ 
		public function getPage(index:int):Page
		{
			for(var i:int = 0; i < pages.length; i++)
			{
				var p:Page = pages.getItemAt(i) as Page;
				if(p.pageNumber == index) return p;
			}
			return null;
		}
		
		[Bindable("dataChange")]
		/**
		 * Gets the cover page of a given magazine
		 * 
		 * @return 
		 * 		the page with smallest page number property
		 */ 
		public function get coverPage():Page
		{
			var firstPageIndex:int = int.MAX_VALUE;
			for(var i:int = 0; i < pages.length; i++)
			{
				if(firstPageIndex > Page(pages.getItemAt(i)).pageNumber)
				{
					firstPageIndex = Page(pages.getItemAt(i)).pageNumber;
				}
			}
			
			if(firstPageIndex == int.MAX_VALUE) return null;
			else return getPage(firstPageIndex);
		}
		
		/**
		 * Sorts the pages of the magazine
		 */
		public function sortPages():void
		{
			var sort:Sort = new Sort();
			sort.fields = [new SortField("pageNumber")]; 
			//sort.compareFunction = myCompare; 
			pages.sort = sort;
			pages.refresh();
			
			function myCompare(a:Object, b:Object, fields:Array = null):int 
			{ 
				if((a as Page).pageNumber > (b as Page).pageNumber)
				{
					return 1;
				}
				else return -1;
			} 
		}
		
		[Bindable("dataChange")]
		/**
		 * Returns the size (in number of pages) of magazine
		 */
		public function get size():int
		{
			return pages.length;
		}
	}
}